﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CongressVote.Models;
using System.Web.Security;

namespace CongressVote.Controllers
{ 
    public class _UserController : Controller
    {
        private finalEntities db = new finalEntities();

        //
        // GET: /_User/

        public ViewResult Index()
        {
            var users = db.Users.Include(u => u.District).Include(u => u.Party).Include(u => u.State);
            return View(users.ToList());
        }

        //
        // GET: /_User/Details/5

        public ViewResult Details(int id)
        {
            Users users = db.Users.Find(id);
            return View(users);
        }

        //
        // GET: /_User/Create

        public ActionResult Create()
        {
            ViewBag.DistrictId = new SelectList(db.District, "id", "name");
            ViewBag.PartyId = new SelectList(db.Party, "id", "name");
            ViewBag.StateId = new SelectList(db.State, "id", "name");
            return View();
        } 

        //
        // POST: /_User/Create

        [HttpPost]
        public ActionResult Create(Users users)
        {
            if (ModelState.IsValid)
            {
                /*     Fady code          */
                var crypto = new SimpleCrypto.PBKDF2();
                var encrpPass = crypto.Compute(users.userPassword);
                //Set pass encryption and user type//
                users.userName = users.userName;
                users.userPassword = encrpPass;
                users.password_salt = crypto.Salt;
                users.User_Type = 2;
                /****************************/
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }
        
        //
        // GET: /_User/Edit/5
 
        public ActionResult Edit(int id)
        {
            Users users = db.Users.Find(id);
            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }

        //
        // POST: /_User/Edit/5

        [HttpPost]
        public ActionResult Edit(Users users)
        {
            if (ModelState.IsValid)
            {
                db.Entry(users).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }

        //
        // GET: /_User/Delete/5
 
        public ActionResult Delete(int id)
        {
            Users users = db.Users.Find(id);
            return View(users);
        }

        //
        // POST: /_User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Users users = db.Users.Find(id);
            db.Users.Remove(users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }




        /*           javaboy login                 */


        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");

        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        /*[HttpPost]
        public ActionResult Login(CongressVote.Models.Users usermodel)
        {
            if (ModelState.IsValid)
            {
                if (Isvalid(usermodel.userName, usermodel.userPassword))
                {
                    FormsAuthentication.SetAuthCookie(usermodel.userName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect.");
                }
            }

            return View(usermodel);
        }
         * 
        private bool Isvalid(string username, string password)
        {
            bool Isvalid = false;
            var crypto = new SimpleCrypto.PBKDF2();

            Users user =
                db.Users.FirstOrDefault(m => m.userName == username);
            if (user != null)
            {
                if (user.userPassword == crypto.Compute(password,user.password_salt))
                {
                    Isvalid = true;
                }
            }

            return Isvalid;
        }*/
        //  end  javaboy code
        /****************************************************************/
        
        /*          fady login        */


        [HttpPost]
        public ActionResult Login(CongressVote.Models.Users user)
        {
            if (ModelState.IsValid)
            {
                if (IsValid(user.userName, user.userPassword) >= 1)
                {
                    FormsAuthentication.SetAuthCookie(user.userName, false);
                    var user_test = db.Users.FirstOrDefault(u => u.userName == user.userName);

                    Session["UserID"] = user_test.id;
                    Session["UserType"] = user_test.User_Type;
                    if (IsValid(user.userName, user.userPassword) == 3)
                    {
                        return RedirectToAction("Index", "Admin");
                    }
                    else if (IsValid(user.userName, user.userPassword) == 2)
                    {
                        //not implemented yet redirct to senator
                        return RedirectToAction("Index", "Home");
                    }
                    else if (IsValid(user.userName, user.userPassword) == 1) 
                    {
                        //not implemented yet redirct to representitive
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect");
                }
            }
            return View();
        }

        private int IsValid(string Username, string Password)
        {
            var crypto = new SimpleCrypto.PBKDF2();

            int isValid = 0;

            using (var db = new finalEntities())
            {
                var user_test = db.Users.FirstOrDefault(u => u.userName == Username);

                if (user_test != null)
                {
                    if (user_test.userPassword == crypto.Compute(Password, user_test.password_salt))
                    {
                        if (user_test.User_Type == 1) //repersentative
                        {
                            isValid = 1;
                        }
                        else if (user_test.User_Type == 2) //Senator
                        {
                            isValid = 2;
                        }
                        else if (user_test.User_Type == 3) //Admin
                        {
                            isValid = 3;
                        }
                        else
                        {
                            isValid = 0;//not log in
                        }
                    }
                }
            }
            return isValid;
        }


        /********************************************/

        /*          Fady code         */
        // GET: /User/Create
        [HttpGet]
        public ActionResult CreateSenator()
        {
            ViewBag.DistrictId = new SelectList(db.District, "id", "name");
            ViewBag.PartyId = new SelectList(db.Party, "id", "name");
            ViewBag.StateId = new SelectList(db.State, "id", "name");
            return View();
        }
        [HttpGet]
        public ActionResult CreateRepresentative()
        {
            ViewBag.DistrictId = new SelectList(db.District, "id", "name");
            ViewBag.PartyId = new SelectList(db.Party, "id", "name");
            ViewBag.StateId = new SelectList(db.State, "id", "name");
            return View();
        }
        [HttpGet]
        public ActionResult CreateAdmin()
        {
            ViewBag.DistrictId = new SelectList(db.District, "id", "name");
            ViewBag.PartyId = new SelectList(db.Party, "id", "name");
            ViewBag.StateId = new SelectList(db.State, "id", "name");
            return View();
        }
        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult CreateSenator(Users users)
        {
            if (ModelState.IsValid)
            {
                /*     Fady code          */
                var crypto = new SimpleCrypto.PBKDF2();
                var encrpPass = crypto.Compute(users.userPassword);
                //Set pass encryption and user type//
                users.userName = users.userName;
                users.userPassword = encrpPass;
                users.password_salt = crypto.Salt;
                users.User_Type = 2;
                /****************************/
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }

        [HttpPost]
        public ActionResult CreateAdmin(Users users)
        {
            if (ModelState.IsValid)
            {
                /*     Fady code          */
                var crypto = new SimpleCrypto.PBKDF2();
                var encrpPass = crypto.Compute(users.userPassword);
                //Set pass encryption and user type//
                users.userName = users.userName;
                users.userPassword = encrpPass;
                users.password_salt = crypto.Salt;
                users.User_Type = 3;
                /****************************/
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }
        [HttpPost]
        public ActionResult CreateRepresentative(Users users)
        {
            if (ModelState.IsValid)
            {
                /*     Fady code          */
                var crypto = new SimpleCrypto.PBKDF2();
                var encrpPass = crypto.Compute(users.userPassword);
                //Set pass encryption and user type//
                users.userName = users.userName;
                users.userPassword = encrpPass;
                users.password_salt = crypto.Salt;
                users.User_Type = 2;
                /****************************/
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DistrictId = new SelectList(db.District, "id", "name", users.DistrictId);
            ViewBag.PartyId = new SelectList(db.Party, "id", "name", users.PartyId);
            ViewBag.StateId = new SelectList(db.State, "id", "name", users.StateId);
            return View(users);
        }
    /***********************************************************/

    }
}